package com.efast.dmom.mgmt.service.les.impl;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.les.LocDlocWhBean;
import com.efast.dmom.mgmt.dao.les.TwActTranLogDao;
import com.efast.dmom.mgmt.dao.les.TwAdjDetailsDao;
import com.efast.dmom.mgmt.dao.les.TwAdjHeaderDao;
import com.efast.dmom.mgmt.dao.les.TwInvSkuLoctlDao;
import com.efast.dmom.mgmt.dao.master.TmBasLocDao;
import com.efast.dmom.mgmt.entity.les.TwActTranLog;
import com.efast.dmom.mgmt.entity.les.TwAdjDetails;
import com.efast.dmom.mgmt.entity.les.TwAdjHeader;
import com.efast.dmom.mgmt.entity.les.TwInvSkuLoctl;
import com.efast.dmom.mgmt.entity.master.TmBasLoc;
import com.efast.dmom.mgmt.service.les.TwAdjHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author chlp
 */
@Service
public class TwAdjHeaderServiceImpl implements TwAdjHeaderService {

	/**
	 * 库存调整单头表 TW_ADJ_HEADER
	 */
	private final TwAdjHeaderDao twAdjHeaderDao;
	/**
	 * 库存调整单子表 TW_ADJ_DETAILS
	 */
	private final TwAdjDetailsDao twAdjDetailsDao;
	/**
	 * 库存表TW_INV_SKU_LOC_T_L
	 */
	private final TwInvSkuLoctlDao twInvSkuLoctlDao;
	/**
	 * 库存交易表TW_ACT_TRAN_LOG
	 */
	private final TwActTranLogDao twActTranLogDao;
	/**
	 * 库位表 tm_bas_loc
	 */
	private final TmBasLocDao tmBasLocDao;
	@Qualifier("jdbcDao")
	@Autowired
	private JdbcDao jdbcDao;

	@Autowired
	public TwAdjHeaderServiceImpl(TwAdjHeaderDao twAdjHeaderDao, TwAdjDetailsDao twAdjDetailsDao,
			TwInvSkuLoctlDao twInvSkuLoctlDao, TwActTranLogDao twActTranLogDao, TmBasLocDao tmBasLocDao) {
		this.twAdjHeaderDao = twAdjHeaderDao;
		this.twAdjDetailsDao = twAdjDetailsDao;
		this.twInvSkuLoctlDao = twInvSkuLoctlDao;
		this.twActTranLogDao = twActTranLogDao;
		this.tmBasLocDao = tmBasLocDao;
	}

	/**
	 * TODO 新增库存调整单 头
	 */
	@Override
	public int insertNonEmptyTwAdjHeader(TwAdjHeader value) {
		String adjSheetNo = jdbcDao.executeProcGetCode("tw_adj_header", "efast");
		value.setAdjSheetNo(adjSheetNo);
		// 调整时间
		value.setAdjTime(new Date());
		// 单据状态:0新单,1发布,2完成,3关闭,4部分调整
		value.setSheetStatus("0");
		// 打印状态0未打1打印完成
		value.setPrintStatus("0");
		value.setMarkStatus(1);
		value.setCreateTime(new Date());
		value.setCreateUser(SpringWebUtils.getRequestUser());
		value.setCompanyCode(SpringWebUtils.getRequestCompany());
		return twAdjHeaderDao.insertNonEmptyTwAdjHeader(value);
	}

	/**
	 * TODO 库存调整单 执行
	 */
	@Override
	public int updateNonEmptyTwAdjHeaderById(TwAdjHeader enti) {
		// 【1】：根据headerid查询出对应的detail
		List<TwAdjDetails> listTwAdjDetails = twAdjDetailsDao.selectTwAdjDetails(
				new Assist(Assist.andEq("tw_adj_header_id", enti.getTwAdjHeaderId())));

		// 【2】：遍历detail，对每个零件进行库存的调整
		for (TwAdjDetails twAdjDetails : listTwAdjDetails) {
			// 非关闭状态的detail才能进行调整
			// 单据状态:0新单,1发布,2完成,3关闭,4部分调整
			if (!"3".equals(twAdjDetails.getLineStatus())) {

				// 【3】：根据库位编码，查询库位记录
				List<TmBasLoc> listTmBasLoc = tmBasLocDao.selectTmBasLoc(
						new Assist(Assist.andEq("storage_no", twAdjDetails.getLocNo())));

				// 【4】：更新库存
				TwInvSkuLoctl twInvSkuLoctl = new TwInvSkuLoctl();
				if (twAdjDetails.getPartPackSerial() != null && twAdjDetails.getInbSerialNo() != null) {
					Assist assist = new Assist();
					assist.setRequires(Assist.andEq("part_no_version", twAdjDetails.getPartNoVersion()));
					assist.setRequires(Assist.andEq("suppl_no", twAdjDetails.getSupplNo()));
					assist.setRequires(Assist.andEq("loc_no", twAdjDetails.getLocNo()));
					assist.setRequires(Assist.andEq("part_pack_serial", twAdjDetails.getPartPackSerial()));
					assist.setRequires(Assist.andEq("inb_serial_no", twAdjDetails.getInbSerialNo()));
					List<TwInvSkuLoctl> twInvSkuLoctls = twInvSkuLoctlDao.selectTwInvSkuLoctl(assist);
					if (twInvSkuLoctls.size() == 0) {
						throw new ServiceException(twAdjDetails.getLocNo() + "没有库存记录，无法调整库存");
					}
					twInvSkuLoctl.setTwInvSkuLoctlId(twInvSkuLoctls.get(0).getTwInvSkuLoctlId());
					// 库存数量 -->调整数量
					twInvSkuLoctl.setCurQty(twInvSkuLoctls.get(0).getCurQty() + twAdjDetails.getAdjQty());
					// 可用数量
					twInvSkuLoctl.setAvailableQty(twInvSkuLoctls.get(0).getAvailableQty() + twAdjDetails.getAdjQty());
					// 箱数
					twInvSkuLoctl.setPackageNum(twAdjDetails.getAdjPackNum());
				}
				if (twAdjDetails.getPartPackSerial() == null && twAdjDetails.getInbSerialNo() == null) {
					Assist assist = new Assist();
					assist.setRequires(Assist.andEq("part_no_version", twAdjDetails.getPartNoVersion()));
					assist.setRequires(Assist.andEq("suppl_no", twAdjDetails.getSupplNo()));
					assist.setRequires(Assist.andEq("loc_no", twAdjDetails.getLocNo()));
					List<TwInvSkuLoctl> twInvSkuLoctls = twInvSkuLoctlDao.selectTwInvSkuLoctl(assist);
					if (twInvSkuLoctls.size() == 0) {
						throw new ServiceException(twAdjDetails.getLocNo() + "没有库存记录，无法调整库存");
					}
					twInvSkuLoctl.setTwInvSkuLoctlId(twInvSkuLoctls.get(0).getTwInvSkuLoctlId());
					// 库存数量 -->调整数量
					twInvSkuLoctl.setCurQty(twInvSkuLoctls.get(0).getCurQty() + twAdjDetails.getAdjQty());
					// 可用数量
					twInvSkuLoctl.setAvailableQty(twInvSkuLoctls.get(0).getAvailableQty() + twAdjDetails.getAdjQty());
					// 箱数
					twInvSkuLoctl.setPackageNum(twAdjDetails.getAdjPackNum());
				}
				twInvSkuLoctlDao.updateNonEmptyTwInvSkuLoctlById(twInvSkuLoctl);

				// 【5】：插入 库存交易表TW_ACT_TRAN_LOG
				// 通过库位id，找到库区，仓库。
				LocDlocWhBean locDlocWhBean = twInvSkuLoctlDao.queryParentNo(listTmBasLoc.get(0).getTmBasLocId());
				TwActTranLog twActTranLog = new TwActTranLog();
				BeanUtils.copyProperties(twAdjDetails, twActTranLog);
				// 操作人
				twActTranLog.setOperator(SpringWebUtils.getRequestUser());
				// 交易时间
				twActTranLog.setTransTime(new Date());
				// 是否允许上传SAP0不允许1允许
				twActTranLog.setIsCanTosap("1");
				// 1为已生成接口数据
				twActTranLog.setFlag(0);
				// 是否已上传SAP
				twActTranLog.setIsTosap("0");
				// 交易类型 （入库0、分配1、拣货2、出库3、移动4、调整5）
				twActTranLog.setTransType("5");
				// twActTranLog.setCostCenter();//成本中心
				// twActTranLog.setDocLineno();//交易涉及单证行号
				// twActTranLog.setTransCode();//移动代码
				// twActTranLog.setDStorageNo();//SAP目的存储地点

				// twActTranLog.setFmCustomerid();//From 客户
				// twActTranLog.setTransReasonCode();//移动原因代码
				// twActTranLog.setFmPackid();//原始包装
				// twActTranLog.setInnerOrder();//内部订单
				// 原始库位交易数量
				twActTranLog.setFmQty(twAdjDetails.getQty());
				// 原始仓库
				twActTranLog.setFmWarehouse(locDlocWhBean.getWarehomeNo());
				// 原始库区
				twActTranLog.setFmArea(locDlocWhBean.getDlocNo());
				// 原始库位
				twActTranLog.setFmLocation(locDlocWhBean.getLocNo());
				// From 产品
				twActTranLog.setFmPartNo(twInvSkuLoctl.getPartNoVersion());
				// From 供应商号
				twActTranLog.setFmSupplierNo(twInvSkuLoctl.getSupplNo());
				// 基本单位
				twActTranLog.setBaseUnit(twInvSkuLoctl.getBaseUnit());
				// 原始库位单位
				twActTranLog.setFmUom(twInvSkuLoctl.getBaseUnit());
				// 原始跟踪号
				twActTranLog.setFmId(twInvSkuLoctl.getInbSerialNo());
				// 原始批次号
				twActTranLog.setFmLotnum(twInvSkuLoctl.getLotNo());

				// twActTranLog.setToPackid();//目标包装ID
				// twActTranLog.setToCustomerid();//To 客户
				// 目标库位交易数量
				twActTranLog.setToQty(twAdjDetails.getAdjQty());
				// 目标仓库
				twActTranLog.setToWarehouse(locDlocWhBean.getWarehomeNo());
				// 目标库区
				twActTranLog.setToArea(locDlocWhBean.getDlocNo());
				// 目标库位
				twActTranLog.setToLocation(locDlocWhBean.getLocNo());
				// 目标跟踪号
				twActTranLog.setToId(twInvSkuLoctl.getPartPackSerial());
				// To 供应商号
				twActTranLog.setToSupplierNo(twInvSkuLoctl.getSupplNo());
				// 目标批次号
				twActTranLog.setToLotnum(twInvSkuLoctl.getLotNo());
				// 目标单位
				twActTranLog.setToUom(twInvSkuLoctl.getBaseUnit());
				// To 产品
				twActTranLog.setToPartNo(twInvSkuLoctl.getPartNoVersion());

				// 库存交易涉及单据类型（到货通知单0、上架作业单1、拣货作业单2、补货作业单3、翻包作业单4、装箱作业单5、装车发运单6、出库订单7、库存调整单8、库存移动单9）
				twActTranLog.setDocType("8");
				// 交易涉及单据号
				twActTranLog.setDocno(enti.getAdjSheetNo());
				// twActTranLog.setReasonCode();//原因代码
				// twActTranLog.setInterfaceGroupId();//接口汇总组号
				// twActTranLog.setSapTransType();//移动类型
				// twActTranLog.setAcount();//凭证抬头文本
				// twActTranLog.setSStorageNo();//SAP源存储地点
				// twActTranLog.setEcrno();//ECR编号
				// twActTranLog.setReasonDesc();//原因
				// twActTranLog.setNotes();//备注
				// 备注1(工厂)
				twActTranLog.setNote1(twAdjDetails.getPlantNo());
				// 备注2（车间）
				twActTranLog.setNote2(twAdjDetails.getWorkshopNo());
				// twActTranLog.setNote3();//备注3
				// twActTranLog.setNote4();//备注4
				// twActTranLog.setNote5();//备注5
				// twActTranLog.setNote6();//备注6
				// twActTranLog.setNote7();//备注7
				// twActTranLog.setNote8();//备注8
				// twActTranLog.setNote9();//备注9
				// 版本控制
				twActTranLog.setOptCounter(1);
				// 是否激活
				twActTranLog.setMarkStatus(1);
				// 创建时间
				twActTranLog.setCreateTime(new Date());
				// 创建用户
				twActTranLog.setCreateUser(SpringWebUtils.getRequestUser());
				// 企业代码
				twActTranLog.setCompanyCode(SpringWebUtils.getRequestCompany());
				twActTranLogDao.insertNonEmptyTwActTranLog(twActTranLog);
			}
		}
		return twAdjHeaderDao.updateNonEmptyTwAdjHeaderById(enti);
	}

	@Override
	public long getTwAdjHeaderRowCount(Assist assist) {
		return twAdjHeaderDao.getTwAdjHeaderRowCount(assist);
	}

	@Override
	public List<TwAdjHeader> selectTwAdjHeader(Assist assist) {
		return twAdjHeaderDao.selectTwAdjHeader(assist);
	}

	@Override
	public TwAdjHeader selectTwAdjHeaderByObj(TwAdjHeader obj) {
		return twAdjHeaderDao.selectTwAdjHeaderByObj(obj);
	}

	@Override
	public TwAdjHeader selectTwAdjHeaderById(Integer id) {
		return twAdjHeaderDao.selectTwAdjHeaderById(id);
	}

	@Override
	public int insertTwAdjHeader(TwAdjHeader value) {
		return twAdjHeaderDao.insertTwAdjHeader(value);
	}

	@Override
	public int insertTwAdjHeaderByBatch(List<TwAdjHeader> value) {
		return twAdjHeaderDao.insertTwAdjHeaderByBatch(value);
	}

	@Override
	public int deleteTwAdjHeaderById(Integer id) {
		return twAdjHeaderDao.deleteTwAdjHeaderById(id);
	}

	@Override
	public int deleteTwAdjHeader(Assist assist) {
		return twAdjHeaderDao.deleteTwAdjHeader(assist);
	}

	@Override
	public int updateTwAdjHeaderById(TwAdjHeader enti) {
		return twAdjHeaderDao.updateTwAdjHeaderById(enti);
	}

	@Override
	public int updateTwAdjHeader(TwAdjHeader value, Assist assist) {
		return twAdjHeaderDao.updateTwAdjHeader(value, assist);
	}

	@Override
	public int updateNonEmptyTwAdjHeader(TwAdjHeader value, Assist assist) {
		return twAdjHeaderDao.updateNonEmptyTwAdjHeader(value, assist);
	}

}